/**
 * @desc: 智能设置弹框
 * @author: elvis
 */
import React, { useState, useEffect } from "react";
import CommonModal from '@/components/CommonModal';
import CommonLoading from '@/components/CommonLoading';
import { Form, Radio, Select, message } from 'antd';
import { find } from 'lodash';
import { getCurrentUser } from '@/utils/authority';
import { getGoodsSet, getChannelList, setGoodsSet } from '../../service';

const { Option } = Select;

const currentUser = getCurrentUser() || {};

export default ({ visible, handleOk, handleCancel, roleType }) => {

  const [form] = Form.useForm();
  
	//渠道列表
	const [channelList, setChannelList] = useState([]);

  const [loading, setLoading] = useState(false)

  useEffect(() => {
    if(visible) {
      queryGoodsSet();
    }
  }, [visible])

  //获取目前的设置值
  const queryGoodsSet = async () => {
    setLoading(true);
    const { success, data } = await getGoodsSet();
    if(success) {
      const { isGetGoods = 2, commonChannelId, channelName } = data;
      const { data:dataRepeat = [], success:successRepeat } = await getChannelList({
        isDeleted: 0,
        type: 2,
      });
      if (successRepeat) {
        dataRepeat.map(item=>{
          item.channelName = `${item.channelName}(${item.uuid == currentUser.uuid ? '自发' : '第三方'})`
        })
        const obj = find(dataRepeat, ['id', commonChannelId]);
        if(!obj){
          dataRepeat.unshift({channelName, id:commonChannelId})
        }
        setChannelList(dataRepeat);
      }
      form.setFieldsValue({
        commonChannelId,
        type: isGetGoods
      })
    }
    setLoading(false);
  }

  const onOk = () => {
    form.validateFields().then(val => {
      setGoods(val);
    }).catch(err => {
      console.log(err);
    }) 
  }

  //智能商品设置
  const setGoods = async (val) => {
    setLoading(true);
    const { success } = await setGoodsSet({
      ...val
    })
    if (success) {
      message.success('智能设置成功');
      handleOk();
    }
    setLoading(false);
  }

  return (
    <>
      <CommonModal
        title="商品渠道规则智能设置"
        visible={visible}
        onCancel={handleCancel}
        onOk={onOk}
        width={520}
      >
        <div className="form_box">
          <div style={{background:'#EAF5FE', padding: '10px', marginBottom: '12px'}}>
          对商品进行匹配或者更换匹配时，商品渠道会优先按照每个网店配置的规则显示；如果网店里没配置，则会按照这里的规则进行显示
          </div>
          <Form
            name="mind-set-goods"
            form={form}
          >
            <Form.Item
              label="商品类型规则："
              name="type"
            >
              <Radio.Group>
                {roleType == 'daifa' ? '' : <Radio style={{ marginBottom: 15 }} value={1}>已匹配商品自动为自发拿货商品</Radio>}
                <Radio style={{ marginBottom: 15 }} value={2}>已匹配商品自动为代发拿货商品</Radio>
                <Radio value={3}>已匹配的商品仍旧为库存商品不变</Radio>
              </Radio.Group>
            </Form.Item>

            <Form.Item name="commonChannelId" label="常用订单渠道：" rules={[{required:true, message:"请先选择常用订单渠道"}]}>
              <Select placeholder="请选择常用订单渠道">
                { channelList.map(item=><Option key={item.id} value={item.id}>{item.channelName}</Option>) }
              </Select>
            </Form.Item>
          </Form>
        </div>
      </CommonModal>
      <CommonLoading loading={loading} />
    </>
  )
}